package com.hserver.deploy.service;

import com.hserver.deploy.bean.JavaDeploy;
import com.hserver.deploy.dao.JavaDeployMapper;
import com.hserver.deploy.utils.CmdMsg;
import com.hserver.deploy.utils.ShellUtil;
import com.hserver.deploy.utils.StringUtils;
import top.hserver.core.ioc.annotation.Autowired;
import top.hserver.core.ioc.annotation.Bean;

import java.io.File;
import java.io.IOException;
import java.util.List;


@Bean
public class IndexService {

    @Autowired
    private JavaDeployMapper javaDeployMapper;

    public void select() {
        Object s = javaDeployMapper.all();
    }


    private String shellFileFolder = System.getProperty("user.dir") + File.separator + "shell";

    private String basePath = System.getProperty("user.dir")+File.separator+"data";

    public List<JavaDeploy> getList() {
        return javaDeployMapper.all();
    }

    public JavaDeploy getDetail(String uuid) {
        return javaDeployMapper.getDetail(uuid);
    }

    public void del(String uuid) {
        javaDeployMapper.deleteById(uuid);
    }

    public void insert(JavaDeploy javaDeployInfo) {
        javaDeployMapper.insert(javaDeployInfo);
    }

    public String getStatus(String uuid) throws IOException {
        JavaDeploy info = javaDeployMapper.getDetail(uuid);
        if (info != null) {
            String out = ShellUtil.exec(null, "sh " + shellFileFolder + "/isrunning.sh " + info.getUuid());
            return String.valueOf(StringUtils.hasText(out) && out.contains("java -jar"));
        } else {
            return "false";
        }
    }

    public void deploy(CmdMsg msg, String uuid) throws IOException {
        JavaDeploy info = javaDeployMapper.getDetail(uuid);
        if (info != null) {
            // kill进程
            ShellUtil.exec(msg, "sh " + shellFileFolder + "/kill.sh " + info.getUuid());
            // 打包
            String[] cmdArray = {"sh", shellFileFolder + "/package.sh", info.getUuid(), info.getUrl(), basePath, String.valueOf(info.getType()), info.getProfile(), info.getBranch()};
            ShellUtil.exec(msg, cmdArray);
            String finalName = getFinalName(info.getUuid());
            if (finalName != null) {
                String module = "";
                if (StringUtils.hasText(info.getModule())) {
                    module = module + '/';
                }
                // 启动程序
                ShellUtil.exec(msg, "sh " + shellFileFolder + "/start.sh " + info.getUuid() + " " + finalName + " " + basePath + " " + module);
            } else {
                msg.result("打包失败");
            }
        } else {
            msg.result(uuid + "对应的项目不存在！");
        }
    }

    public String restart(String uuid) throws IOException {

        JavaDeploy info = javaDeployMapper.getDetail(uuid);

        if (info != null) {
            StringBuilder sb = new StringBuilder();
            // kill进程
            sb.append(ShellUtil.exec(null, "sh " + shellFileFolder + "/kill.sh " + info.getUuid()));
            String finalName = getFinalName(info.getUuid());
            if (finalName != null) {
                String module = "";
                if (StringUtils.hasText(info.getModule())) {
                    module = module + '/';
                }
                // 启动程序
                sb.append(ShellUtil.exec(null, "sh " + shellFileFolder + "/start.sh " + info.getUuid() + " " + finalName + " " + basePath + " " + module));
            } else {
                sb.append("找不到程序包，请重新部署");
            }
            return sb.toString();
        } else {
            return uuid + "对应的项目不存在！";
        }
    }

    public String stop(String uuid) throws IOException {
        JavaDeploy info = javaDeployMapper.getDetail(uuid);
        if (info != null) {
            System.out.println("sh " + shellFileFolder + "/kill.sh " + info.getUuid());
            return ShellUtil.exec(null, "sh " + shellFileFolder + "/kill.sh " + info.getUuid());
        } else {
            return uuid + "对应的项目不存在！";
        }
    }

    public String showLog(String uuid) throws IOException {
        JavaDeploy info = javaDeployMapper.getDetail(uuid);
        if (info != null) {
           return  ShellUtil.exec(null, "sh " + shellFileFolder + "/showlog.sh " + info.getUuid() + " " + basePath);
        } else {
          return   ShellUtil.exec(null, "echo \"对应的项目不存在！\"");
        }
    }

    private String getFinalName(String uuid) {
        File dir = new File(basePath + "/" + uuid + "/target");
        File[] files = dir.listFiles();

        String fileName = null;
        long size = 0;
        for (File file : files) {
            String name = file.getName();
            if (file.isFile() && name.endsWith(".jar")) {
                if (file.length() > size) {
                    fileName = name;
                    size = file.length();
                }
            }
        }
        return fileName;
    }

}
